﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using Common.Logging;
using Quartz;
using Quartz.Impl;
using System.IO;

namespace QuartzJobFramework
{
    public partial class Service1 : ServiceBase
    {
        private readonly ILog logger;
        private IScheduler scheduler;
        public Service1()
        {
            InitializeComponent();
            try
            { 
                logger = LogManager.GetLogger(GetType());
                ISchedulerFactory schedulerFactory = new StdSchedulerFactory();              
                scheduler = schedulerFactory.GetScheduler();
               
            }
            catch(Exception e){
                using (var fs = new System.IO.FileStream(@"d:\mcWindowsService.txt", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write)) {
                    System.IO.StreamWriter m_streamWriter = new System.IO.StreamWriter(fs);
                    m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
                    m_streamWriter.WriteLine("exect" + e.Message);
                    m_streamWriter.Flush();
                    m_streamWriter.Close();
                }
         
            }
        }

        protected override void OnStart(string[] args)
        { 
            scheduler.Start();
            logger.Info("Quartz服务成功启动");
        }

        protected override void OnStop()
        {
            scheduler.Shutdown();
            logger.Info("Quartz服务成功终止");
        }

        protected override void OnPause()
        {
            scheduler.PauseAll();
        }

        protected override void OnContinue()
        {
            scheduler.ResumeAll();
        }
    }
}
